#!/usr/bin/python
#
# Copyright (C) 2010 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" @file
@brief Implements RamsService.
"""

try:
  from xml.etree import cElementTree as ElementTree
except ImportError:
  try:
    import cElementTree as ElementTree
  except ImportError:
    from elementtree import ElementTree
import os
import urllib
import ratom
import rawsc
import rawsc.service
from rawsc import rams
from rawsc.rams import traffic, geo, total, concurrent, ttype, storage, users, used, payed

SERVER_DEFAULT = "rams.mon02.rambla.be"

class RamsService(rawsc.service.RawsService):
    """ @brief RamsService extends RawsService to provide an interface for RAMS requests.
    """

    def __init__(self, username=None, password=None, source=None, server=SERVER_DEFAULT, port = None, additional_headers=None, handler=None, ssl = False):
        super(RamsService, self).__init__(username = username, password = password, source = source, server = server, port = port, additional_headers = additional_headers, handler = handler, ssl = ssl)

    def delete(self, uri):
        """ Deletes any resource, given the uri. """
        return self.Delete(uri = uri)

    def getTrafficFeed(self, query = None, full_uri = None):
        """ Retrieves an traffic feed. 
        
            @param query rawsc.Query object that contains query-string args.
            @param full_uri string Full URI already containing the query-string args (if not None -> query param is ignored).
            @return TrafficFeed object
        """
        uri = "/traffic/"
        if full_uri is not None:
            uri = full_uri
        elif query:
            query.feed = uri
            uri = query.ToUri()
        return self.Get(uri = uri, converter=rams.traffic.TrafficFeedFromString)
        
    def getTrafficTypeFeed(self):
        """ Retrieves a traffic-type feed. 

            @return TtypeFeed object
        """
        uri = "/traffic-type/"
        return self.Get(uri = uri, converter=rams.ttype.TtypeFeedFromString)

    def getStorageFeed(self, query = None):
        """ Retrieves a storage feed. 

            @param query rawsc.Query object that contains query-string args.
            @return StorageFeed object
        """
        uri = "/storage/"
        if query:
            query.feed = uri
            uri = query.ToUri()
        return self.Get(uri = uri, converter=rams.storage.StorageFeedFromString)

    def getTotalFeed(self, query = None, full_uri = None):
        """ Retrieves an total feed. 

            @param query rawsc.Query object that contains query-string args.
            @param full_uri string Full URI already containing the query-string args (if not None -> query param is ignored).
            @return TotalFeed object
        """
        uri = "/total/"
        if full_uri is not None:
            uri = full_uri
        elif query:
            query.feed = uri
            uri = query.ToUri()
        return self.Get(uri = uri, converter=rams.total.TotalFeedFromString)

    def getConcurrentFeed(self, query = None, full_uri = None):
        """ Retrieves an concurrent feed. 

            @param query rawsc.Query object that contains query-string args.
            @param full_uri string Full URI already containing the query-string args (if not None -> query param is ignored).
            @return ConcurrentFeed object
        """
        uri = "/concurrent/v1/"
        if full_uri is not None:
            uri = full_uri
        elif query:
            query.feed = uri
            uri = query.ToUri()
        return self.Get(uri = uri, converter=rams.concurrent.ConcurrentFeedFromString)

    def getHostFeed(self, query = None, full_uri = None):
        """ Retrieves an host feed from the geo resource. 

            @param query rawsc.Query object that contains query-string args.
            @param full_uri string Full URI already containing the query-string args (if not None -> query param is ignored).
            @return HostFeed object
        """
        return self._get_geo_feed(resource = "/geo/host/", query = query, full_uri = full_uri)

    def getDomainFeed(self, query = None, full_uri = None):
        """ Retrieves an domain feed from the geo resource. 

            @param query rawsc.Query object that contains query-string args.
            @param full_uri string Full URI already containing the query-string args (if not None -> query param is ignored).
            @return DomainFeed object
        """
        return self._get_geo_feed(resource = "/geo/domain/", query = query, full_uri = full_uri)

    def getCityFeed(self, query = None, full_uri = None):
        """ Retrieves an domain feed from the geo resource. 

            @param query rawsc.Query object that contains query-string args.
            @param full_uri string Full URI already containing the query-string args (if not None -> query param is ignored).
            @return CityFeed object
        """
        return self._get_geo_feed(resource = "/geo/city/", query = query, full_uri = full_uri)
        
    def getUsersFeed(self):
        """ Retrieves a users feed. 

            @return UsersFeed object
        """
        uri = "/customer/users/"
        return self.Get(uri = uri, converter=rams.users.UsersFeedFromString)

    def getUsedFeed(self, query = None):
        """ Retrieves a used feed. 

            @param query rawsc.Query object that contains query-string args.
            @return UsedFeed object
        """
        uri = "/customer/used/"
        if query:
            query.feed = uri
            uri = query.ToUri()
        return self.Get(uri = uri, converter=rams.used.UsedFeedFromString)

    def getPayedFeed(self, query = None):
        """ Retrieves a payed feed. 

            @param query rawsc.Query object that contains query-string args.
            @return PayedFeed object
        """
        uri = "/customer/payed/"
        if query:
            query.feed = uri
            uri = query.ToUri()
        return self.Get(uri = uri, converter=rams.payed.PayedFeedFromString)
    

    def _get_geo_feed(self, resource, query = None, full_uri = None):
        """ Retrieves an host feed from the geo resource. 

            @param resource string Relative path to the resource (= path part of the URI).
            @param query rawsc.Query object that contains query-string args.
            @param full_uri string Full URI already containing the query-string args (if not None -> query param is ignored).
            @return GeoFeed object
        """
        uri = resource
        if full_uri is not None:
            uri = full_uri
        elif query:
            query.feed = uri
            uri = query.ToUri()
        return self.Get(uri = uri, converter=rams.geo.GeoFeedFromString)

